package com.mdd.admin.controller;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IProductService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.ProductCreateValidate;
import com.mdd.admin.validate.ProductUpdateValidate;
import com.mdd.admin.validate.ProductSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ProductListedVo;
import com.mdd.admin.vo.ProductDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/adminapi/product")
@Api(tags = "产品管理")
public class ProductController {

    @Resource
    IProductService iProductService;

    @GetMapping("/list")
    @ApiOperation(value="产品列表")
    public AjaxResult<PageResult<ProductListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated ProductSearchValidate searchValidate) {
        PageResult<ProductListedVo> list = iProductService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="产品详情")
    public AjaxResult<ProductDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        ProductDetailVo detail = iProductService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "产品新增")
    @PostMapping("/add")
    @ApiOperation(value="产品新增")
    public AjaxResult<Object> add(@Validated @RequestBody ProductCreateValidate createValidate) {
        iProductService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "产品编辑")
    @PostMapping("/edit")
    @ApiOperation(value="产品编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody ProductUpdateValidate updateValidate) {
        iProductService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "产品删除")
    @PostMapping("/del")
    @ApiOperation(value="产品删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iProductService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
